package com.example.lottery_system.common.advice;

import com.example.lottery_system.common.errcode.CommonErrorCodeConstant;
import com.example.lottery_system.common.exception.ControllerException;
import com.example.lottery_system.common.exception.ServiceException;
import com.example.lottery_system.common.pojo.CommonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;



@ControllerAdvice
@ResponseBody
public class CommonExceptionReturn  {
    @ExceptionHandler(ControllerException.class)
    public CommonResult<?> exception(ControllerException exception) {
        /**
         * 设置控制层的错误响应
         */
        Integer code = exception.getCode();
        String message = exception.getMsg();
        CommonResult<Object> error = CommonResult.error(code, message);
        return error;
    }

    @ExceptionHandler(ServiceException.class)
    public CommonResult<?> exception(ServiceException exception) {
        /**
         * 设置控制层的错误响应
         */
        Integer code = exception.getCode();
        String message = exception.getMsg();
        CommonResult<Object> error = CommonResult.error(code, message);
        return error;
    }

    @ExceptionHandler(Exception.class)
    public CommonResult<?> exception(Exception exception) {
        Integer code = CommonErrorCodeConstant.INNER_SERVICE_ERROR.getCode();
        String message = exception.getMessage();
        CommonResult<Object> error = CommonResult.error(code, message);
        return error;
    }

}
